package org.ihelpuoo.parser.segment;

import org.ihelpuoo.parser.StringTraveler;

public class StringNode extends SegmentNode
{

	private StringBuffer	content	= new StringBuffer();

	@Override
	void valueOf(StringTraveler st)
	{
		char c = st.next();
		if (StringTraveler.EMPTY_CHAR == c)
			throw new NodeIsUnusefulExcption();
		while (!st.isEnd())
		{
			if (stack().push(c))
			{
				if (stack().isClosed())
				{
					stack().popPair();
					return;
				}
			}
			else
			{
				content.append(c);
				if (st.hasNext(marks.markHead()))
					return;
			}
			c = st.next();
		}
	}

	@Override
	public void clear()
	{
		content = null;
	}

	@Override
	public CharSequence evaluate()
	{
		if (null == content)
			return "";
		return content.toString();
	}

	@Override
	public SegmentNode duplicate()
	{
		StringNode node = new StringNode();
		node.content = content;
		return node;
	}

	@Override
	public String toString()
	{
		return evaluate().toString();
	}

}
